package com.nathan.mq.integration.config;

import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program:
 * @Description:
 * @Author: zhujunjie
 * @Date: 2021/12/9 17:20
 */
@Configuration
public class RedissonConfiguration {

    @Value("${redis.type:standalone}")
    private String type;
    @Value("${redis.standaloneHost:127.0.0.1}")
    private String standaloneHost;
    @Value("${redis.standalonePort:6379}")
    private Integer standalonePort;
    @Value("${redis.minIdle:5}")
    private Integer minIdle;
    @Value("${redis.maxTotal:40}")
    private Integer maxTotal;
    @Value("${redis.maxWaitMillis:}")
    private Integer maxWaitMillis;
    @Value("${redis.minEvictableIdleTimeMillis:}")
    private Integer minEvictableIdleTimeMillis;
    @Value("${redis.numTestsPerEvictionRun:}")
    private Integer numTestsPerEvictionRun;
    @Value("${redis.testOnBorrow:true}")
    private boolean testOnBorrow;
    @Value("${redis.testWhileIdle:false}")
    private boolean testWhileIdle;
    @Value("${redis.sentinelNodes:}")
    private String sentinelNodes;
    @Value("${redis.sentinelMaster:}")
    private String sentinelMaster;
    @Value("${redis.password:}")
    private String password;
    @Value("${redis.connectTimeout:10000}")
    private int connectTimeout;
    @Value("${redis.timeout:3000}")
    private Integer timeout;

    @Bean
    public RedissonClient redissonClient() {
        final Config config = new Config();
        final String prefix = "redis://";
        final String standaloneType = "standalone";
        if (Objects.equals(type, standaloneType)) {
            final String address = prefix + standaloneHost + ":" + standalonePort;
            final SingleServerConfig singleServerConfig = config.useSingleServer()
                .setAddress(address)
                .setConnectionPoolSize(maxTotal)
                .setConnectionMinimumIdleSize(minIdle)
                // 连接超时，单位：毫秒
                .setConnectTimeout(connectTimeout)
                // 同任何节点建立连接时的等待超时。时间单位是毫秒
                .setTimeout(timeout);
            if (StringUtils.isNotBlank(password)) {
                singleServerConfig.setPassword(password);
            }
        } else {
            final StringBuilder sentinelAddressBuilder = new StringBuilder();
            final String[] sentinelNodeArray = StringUtils.split(sentinelNodes, ",");
            for (String node : sentinelNodeArray) {
                sentinelAddressBuilder.append(prefix).append(node).append(",");
            }
            final String sentinelAddress = sentinelAddressBuilder.toString();
            final SentinelServersConfig sentinelServersConfig = config.useSentinelServers().setMasterName(sentinelMaster)
                .addSentinelAddress(sentinelAddress.substring(0, sentinelAddress.length() - 1))
                .setMasterConnectionPoolSize(maxTotal)
                .setMasterConnectionMinimumIdleSize(minIdle)
                .setSlaveConnectionPoolSize(maxTotal)
                .setSlaveConnectionMinimumIdleSize(minIdle)
                .setConnectTimeout(connectTimeout)
                .setTimeout(timeout);
            if (StringUtils.isNotBlank(password)) {
                sentinelServersConfig.setPassword(password);
            }
        }
        return Redisson.create(config);
    }

}
